const path = require('path')
const fs = require('fs')
const {getUpperCamelCaseName, getStrikeName} = require('../utils')

function createView(moduleName, dirName) {
  const componentName = getUpperCamelCaseName(moduleName)
  const strikeName = getStrikeName(moduleName)
  const filePath = path.resolve(__dirname, `../../src/views/${componentName}View.vue`)
  const content = `<script lang="ts" setup>
import ${componentName} from '../components/${dirName}/${componentName}.vue'
</script>
<template>
  <main class="${strikeName}-view">
    <${componentName} />
  </main>
</template>
<style lang="less">
.${strikeName}-view {
  height: 100%;
  background-color: #ecefff;
}
</style>
`
  fs.writeFile(filePath, content, (err) => {
    if (err) throw err
    console.log(`成功创建并写入文件 ${filePath}`)
  })
}

function createComponent(moduleName, dirName) {
  const componentName = getUpperCamelCaseName(moduleName)
  const strikeName = getStrikeName(moduleName)
  const filePath = path.resolve(__dirname, `../../src/components/${dirName}/${componentName}.vue`)
  const content = `<template>
  <div>测试${strikeName}</div>
</template>
<script setup></script>
`
  fs.writeFile(filePath, content, (err) => {
    if (err) throw err
    console.log(`成功创建并写入文件 ${filePath}`)
  })
}

function createModule({moduleName, dirName}) {
  createView(moduleName, dirName)
  createComponent(moduleName, dirName)
}

module.exports = createModule
